from functools import wraps
from flask import g, request, session, jsonify, redirect
from flask_login import current_user

from config import Config


def login_require(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if request.path in Config.URL_WHITE_LIST:
            g.user_id = None
            return func(*args, **kwargs)
        user_id = session.get('user_id')
        if not user_id:
            return jsonify({'code': 400, 'info': '您还未登录账号,请先登录'})
            # return redirect('/user/login')
        else:
            g.user_id = user_id
            return func(*args, **kwargs)

    return wrapper


# def permission_required(needed_perms):
#     def decorator(func):
#         @wraps(func)
#         def wrapper(*args, **kwargs):
#             # 判断权限......
#             user_id = g.user_id
#             user = User.query.filter_by(id=user_id).first()
#             if not user.has_per(needed_perms):
#                 return jsonify({'code': 403, 'info': '无访问权限'})
#             return func(*args, **kwargs)
#
#         return wrapper
#
#     return decorator
